/**
 * 
 */
package com.simtek.simplayer.utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;

import android.content.Context;

import com.simtek.simplayer.entry.YoutubeUrlMeta;

/**
 * @author Hitech
 * 
 */
public class VideoUtils {
//	public static boolean isYoutubeUrl(String link) {
//		try {
//			URL url = new URL(link);
//			String host = url.getHost();
//			Log.d("video", "link host #" + host);
//			if (StringUtils.equalsIgnoreCase(host, "youtube.com")
//					|| StringUtils.equalsIgnoreCase(host, "www.youtube.com")
//					|| StringUtils.equalsIgnoreCase(host, "m.youtube.com")
//					|| StringUtils.equalsIgnoreCase(host, "youtu.be")) {
//				String path = url.getPath();
//				if (StringUtils.equalsIgnoreCase(path, "/watch")
//						|| StringUtils.equalsIgnoreCase(path, "/v/")
//						|| StringUtils.equalsIgnoreCase(path, "/embed/")) {
//					Log.d("video", "link path #" + path);
//					return true;
//				}
//			}
//		} catch (Throwable e) {
//		}
//		return false;
//	}

	/**
	 * get best youtube url base on screen size
	 * 
	 * @param context
	 * @param list
	 * @return
	 */
	public static YoutubeUrlMeta getBestVideoSize(Context context,
			List<YoutubeUrlMeta> list) {
		YoutubeUrlMeta bestUrl = null;

		if (list != null && !list.isEmpty()) {
			Collections.sort(list);
			int[] size = SystemUtils.getDisplaySize(context);
			if (size != null) {
				int baseSize = (size[0] + size[1]) / 2;
				int lastDiff = Integer.MAX_VALUE;
				for (YoutubeUrlMeta meta : list) {
					int baseMetaSize = (meta.width + meta.height) / 2;
					int currentDiff = Math.abs(baseMetaSize - baseSize);
					if (currentDiff < lastDiff) {
						lastDiff = currentDiff;
						bestUrl = meta;
					}
				}
			}
		}

		return bestUrl;
	}

	public static String getYoutubeCode(String url) {
		URL u;
		try {
			u = new URL(url);
		} catch (MalformedURLException e) {
			return null;
		}
		String host = u.getHost();
		if (!host.equals("youtube.com") && !host.equals("youtu.be") && !host.endsWith(".youtube.com") && !host.endsWith(".youtu.be")) {
			return null;
		}

		String query = u.getQuery();
		if (StringUtils.isNotBlank(query)) {
			int startIdx = query.indexOf("v=");
			if (startIdx > -1) {
				startIdx += 2;
				query += "&";
				int endIdx = query.indexOf("&", startIdx);
				if (endIdx > -1) {
					String code = query.substring(startIdx, endIdx);
					if (StringUtils.isNotBlank(code)) {
						return code;
					}
				}
			}
		}

		String path = u.getPath();
		if (StringUtils.isBlank(path)) {
			return null;
		}

		String[] parts = path.split("/");
		String code = parts[parts.length - 1];
		if (StringUtils.isBlank(code) || code.length() < 5 || "null".equals(code) || "undefined".equals(code)) {
			return null;
		}

		return code;

//		String pattern = "(?<=watch\\?v=|/videos/|youtu.be/|embed\\/)[^#\\&\\?]*";
//		Pattern compiledPattern = Pattern.compile(pattern);
//		Matcher matcher = compiledPattern.matcher(url);
//
//		if (matcher.find()) {
//			String youtubeCode = matcher.group();
//
//			Log.d("video", "extract youtube code #" + youtubeCode);
//
//			return youtubeCode;
//		}
//		return null;
	}

	public static String getYoutubeUrl(String youtubeCode) {
		return null;
	}
}
